package it.unisannio.lesim.fsm;

import java.util.Hashtable;

/**
 *
 * @author Marco Calisti
 * @author Antonio Cuomo
 * @author Rocco Mazzeo
 * @author Francesco Pacilio
 * 
 */

class ExpressionEvaluator {

    boolean evaluateExpression(String fieldExp, Hashtable fields) {
        int operatorIndex = fieldExp.indexOf("AND");
        if (operatorIndex == -1) {
            int equalIndex = fieldExp.indexOf('=');
            if (equalIndex == -1) {
                return false;
            }
            String fieldName = fieldExp.substring(0, equalIndex).trim();
            String fieldValue = fieldExp.substring(equalIndex + 1).trim();
            try {
                FieldValue actualValue = (FieldValue) fields.get(fieldName);
                return actualValue.getValue().equals(fieldValue);
            } catch (Exception r) {
                return false;
            }
        }

        String firstExpr = fieldExp.substring(0, operatorIndex);
        String remainingExpr = fieldExp.substring(operatorIndex + 3);
        return evaluateExpression(firstExpr, fields) && evaluateExpression(remainingExpr, fields);
    }
    
}

